﻿/*----------------------------------------------------------------
 * 
 * 版权所有：Copyright (C) 2012 
 * 
 * 文件名：EZineController.cs
 * 文件功能描述：
 * 
 * 
 * 作 者：zhangq
 * 邮 箱：zhangqiang@bamabest.com
 * 日 期：2013-03-07 03:47:17
 * 
 * 
 * 修改标识： Author
 * 修改描述： 相关描述
 * 
 * ---------------------------------------------------------------- */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Common.Extensions.OrderBy;
using Common.Extensions.Where;
using DomainModel;
using EmitMapper;
using IManager;
using UI.JsonModel;
using WebPlatform.FiltersAndSession;
using WebPlatform.Models;

namespace WebPlatform.Controllers
{

    /// <summary>
    /// 
    /// </summary>
    public class EZineController : Controller
    {


        public IManagerDocument IMDocument { get; set; }


        public Sessions Sessions { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public ActionResult Index()
        {
            return View();
        }



        [HttpPost]
        public JsonResult EZines(int page = 1, int pageSize = 15)
        {

            var companyId = Sessions.User.CompanyId;
            var p = PredicateExtension.True<DocumentInfo>();
            p = p.And(u => u.CompanyId == companyId);
            var count = IMDocument.LoadCount(p);
            var list = IMDocument.LoadAll(p, page, pageSize,
                new OrderByExpression<DocumentInfo, DateTime>(u => u.UploadDate, true));
            var mapper = ObjectMapperManager.DefaultInstance.GetMapper<DocumentInfo, DocumentVM>();
            var listVM = mapper.MapEnum(list).ToList();
            GridData<DocumentVM> gridData = new GridData<DocumentVM>();
            gridData.Rows = listVM;
            gridData.Total = count;
            return Json(gridData);
        }


        public ActionResult EZine(int id = 0)
        {
            DocumentVM docVM = new DocumentVM();
            if (id >  0)
            {
                var doc = IMDocument.GetDocument(id);
                if (null != doc)
                {
                    var mapper = ObjectMapperManager.DefaultInstance.GetMapper<DocumentInfo, DocumentVM>();
                   docVM = mapper.Map(doc);
                }
            }
            if (string.IsNullOrEmpty(docVM.DocUrl))
            {
                docVM.DocUrl = "/upload/71cb1c5e-be99-4229-8e01-91dd401e45b5.pdf.swf";
            }
            return View(docVM);
        }



        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="doc"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult EZine(DocumentVM doc)
        {
            var flag = false;
            if (ModelState.IsValid)
            {
                var mapper = ObjectMapperManager.DefaultInstance.GetMapper<DocumentVM, DocumentInfo>();
                var docInfo = mapper.Map(doc);
                docInfo.CompanyId = Sessions.User.CompanyId;

                if (docInfo.Id == 0)
                {
                    try
                    {
                        docInfo.UploadDate = DateTime.Now;
                        flag = IMDocument.AddDocument(docInfo);
                    }
                    catch (Exception)
                    {

                        //throw;
                    }


                }
                else
                {
                    flag = IMDocument.UpdateDocument(docInfo);
                }
            }
            return Json(flag);
        }


        [HttpPost]
        public JsonResult DeleteDocs(string ids)
        {
            var b = false;
            if (!string.IsNullOrEmpty(ids))
            {
                var idList = ids.Split(',');
                var list = new List<int>();
                foreach (var s in idList)
                {
                    list.Add(Convert.ToInt32(s));
                }
                b = IMDocument.DeleteDocument(list);
                if (b) {
                    foreach (var s in idList)
                    {
                       var doc =  IMDocument.GetDocument(Convert.ToInt32(s));
                       if (null != doc)
                       {
                           System.IO.File.Delete(Server.MapPath(doc.DocUrl));
                       }
                    }
                }
            }
            return Json(b);
        }

    }
}
